package com.jmga.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class DataPullerActivity extends Activity {

	public static final Uri CONTENT_URI_PRICES = Uri
			.parse("content://com.jmga.provider.ecogasfinder/price");
	public static final Uri CONTENT_URI_STATIONS = Uri.parse("content://com.jmga.provider.ecogasfinder/station");
	public static final Uri CONTENT_URI_PRICE_TYPES = Uri.parse("content://com.jmga.provider.ecogasfinder/gasType");
	public static final Uri CONTENT_URI_FREQUENCY = Uri.parse("content://com.jmga.provider.ecogasfinder/frequency");

	public static final String KEY_ID = "_id";
	public static final String KEY_DATE = "date";
	public static final String KEY_STATION = "station";
	public static final String KEY_PRICE = "price";
	public static final String KEY_GAS_TYPE = "gasType";
	
	public static final String KEY_DESCRIPTION = "description";
	public static final String KEY_XKEY = "xkey";
	
	public static final String KEY_LABEL = "label";
	public static final String KEY_OPERATOR = "operator";
	public static final String KEY_ADDRESS = "address";
	public static final String KEY_LANE = "lane";
	public static final String KEY_POSTAL_CODE = "postalCode";
	public static final String KEY_PROVINCE = "province";
	public static final String KEY_LOCALITY = "locality";
	public static final String KEY_SALE_TYPE = "saleType";
	public static final String KEY_SENT_TYPE = "sentType";
	public static final String KEY_X_COORD = "Xcoord";
	public static final String KEY_Y_COORD = "Ycoord";
	public static final String KEY_FAVOURITE = "favourite";
	public static final String KEY_REGULAR = "regular";
	public static final String KEY_OBSERVABLE = "observable";
	
	public static final String KEY_TIMES = "times";
	public static final String ROUTE_POINTS = "routePoints";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ContentResolver cr = getContentResolver();

		Cursor cursor = cr.query(CONTENT_URI_PRICES, null, null, null, KEY_ID + " ASC");
		String colNames = "-";
		for(String columnName : cursor.getColumnNames()) {
			colNames += columnName + "-";
		}
//		Log.d(getClass().getName(),colNames);
		FileOutputStream fstream = null;
		String today = "";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			today = sdf.format(new Date((System.currentTimeMillis())));
			File ecogasFolder = new File("/sdcard/ecogas/");
			
			if(ecogasFolder != null || !ecogasFolder.isDirectory()) {
				ecogasFolder.mkdir();
			}
			
			fstream = new FileOutputStream("/sdcard/ecogas/" + today + "prices.csv");
//			fstream.write(colNames.getBytes());

			if (cursor.moveToFirst()) {
				do {
					String row = "\n";
					row += cursor.getString(0);
					row += ";" + cursor.getString(1);
					row += ";" + cursor.getString(2);
					row += ";" + cursor.getString(3);
					row += ";" + cursor.getString(4);
					fstream.write(row.getBytes());
					
//					Log.d(getClass().getName(), "ROW: " + row);
				} while (cursor.moveToNext());
			}
			fstream.close();
			fstream = new FileOutputStream("/sdcard/ecogas/" + today + "stations.csv");
			cursor = cr.query(CONTENT_URI_STATIONS, null, null, null, KEY_ID + " ASC");
			colNames = "-";
			for(String columnName : cursor.getColumnNames()) {
				colNames += columnName + "-";
			}
			Log.d(getClass().getName(),colNames);
//			fstream.write(colNames.getBytes());
			
			if (cursor.moveToFirst()) {
				do {
					String row = "\n";
					row += cursor.getString(0);
					row += ";" + cursor.getString(1);
					row += ";" + cursor.getString(2);
					row += ";" + cursor.getString(3);
					row += ";" + cursor.getString(4);
					row += ";" + cursor.getString(5);
					row += ";" + cursor.getString(6);
					row += ";" + cursor.getString(7);
					row += ";" + cursor.getString(8);
					row += ";" + cursor.getString(9);
					row += ";" + cursor.getString(10);
					row += ";" + cursor.getDouble(11);
					row += ";" + cursor.getDouble(12);
					row += ";" + cursor.getString(13);
					row += ";" + cursor.getString(14);
					row += ";" + cursor.getString(15);
					fstream.write(row.getBytes());
					
					Log.d(getClass().getName(), row);
				} while (cursor.moveToNext());
			}
			fstream.close();
//			fstream = new FileOutputStream("/sdcard/ecogas/" + today + "frequency.csv");
//			cursor = cr.query(CONTENT_URI_FREQUENCY, null, null, null, KEY_ID + " ASC");
//			colNames = "-";
//			for(String columnName : cursor.getColumnNames()) {
//				colNames += columnName + "-";
//			}
//			Log.d(getClass().getName(),colNames);
//			fstream.write(colNames.getBytes());
//			
//			if (cursor.moveToFirst()) {
//				do {
//					String row = "\n";
//					row += cursor.getString(0);
//					row += ";" + cursor.getString(1);
//					row += ";" + cursor.getString(2);
//					fstream.write(row.getBytes());
//					
//					Log.d(getClass().getName(), "ROW: " + row);
//				} while (cursor.moveToNext());
//			}
//			fstream.close();
//			fstream = new FileOutputStream("/sdcard/ecogas/" + today + "GasType.csv");
//			cursor = cr.query(CONTENT_URI_PRICE_TYPES, null, null, null, KEY_ID + " ASC");
//			colNames = "-";
//			for(String columnName : cursor.getColumnNames()) {
//				colNames += columnName + "-";
//			}
//			Log.d(getClass().getName(),colNames);
//			fstream.write(colNames.getBytes());
//			
//			if (cursor.moveToFirst()) {
//				do {
//					String row = "\n";
//					row += cursor.getString(0);
//					row += ";" + cursor.getString(1);
//					row += ";" + cursor.getString(2);
//					fstream.write(row.getBytes());
//					
//					Log.d(getClass().getName(), "ROW: " + row);
//				} while (cursor.moveToNext());
//			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(fstream != null) {
				try {
					fstream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		Intent intent = new Intent(this, DataPusherActivity.class);
		intent.putExtra("TODAY", today);
		this.startActivity(intent);
	}
}